var config = require('../../../config.js')
var http = require('../../../utils/httpHelper.js')
var dateTimePicker = require('../../../utils/dateTimer.js')
Page({
  data: {
    dating: '',
    dating_p: '', //显示时间
    dateTimeArray: '', //时间数组
    startYear: 2000, //最小年份
    endYear: 2050, // 最大年份
    shopId:null,
    userId:null,
    userName:'',
    phone:'',
    remark:'',
    noSave:true
  },
  //事件处理函数
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: options.shopName+'-预约'
    })
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    let userInfo = wx.getStorageSync('user');
    that.setData({
      dating:obj.dateTime,
      dateTimeArray:obj.dateTimeArray,
      shopId:options.id,
      userId:userInfo.userId,
      userName:userInfo.userName,
      phone:userInfo.phonenumber,
    });
  },
  changeDateTime(e) {
    let dateTimeArray = this.data.dateTimeArray,
      {
        type,
        param
      } = e.currentTarget.dataset;
    this.setData({
      [type]: e.detail.value,
      [param]: dateTimeArray[0][e.detail.value[0]] + '-' + dateTimeArray[1][e.detail.value[1]] + '-' + dateTimeArray[2][e.detail.value[2]] + ' ' + dateTimeArray[3][e.detail.value[3]] + ':' + dateTimeArray[4][e.detail.value[4]],
      noSave:false
    });
  },
  changeDateTimeColumn(e) {
    var dateArr = this.data.dateTimeArray,
      {
        type
      } = e.currentTarget.dataset,
      arr = this.data[type];
    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
    this.setData({
      dateTimeArray: dateArr,
      [type]: arr
    });
  },
  nameinput:function(e) {
    this.setData({
      userName: e.detail.value,
      noSave:false
    })
  },
  phoneinput:function(e) {
    this.setData({
      phone: e.detail.value,
      noSave:false
    })
  },
  remarkinput:function(e) {
    this.setData({
      remark: e.detail.value,
      noSave:false
    })
  },
  formSubmit:function (e){
    var val = e.detail.value;
    if(this.data.dating_p!=''&&val.phone!=''){
      var data = {
        shopId:this.data.shopId,
        userName:val.userName,
        phone:val.phone,
        dating:this.data.dating_p,
        remark:val.remark,
        createBy:this.data.userId
      }
      http.httpPut(config.clubApi.createDating,data,function(res){
        if(res.code == '200') {
          wx.showToast({
            title: '预约成功',
            icon: 'success',
            duration: 1000
          });
          setTimeout(function () {
            wx.navigateBack();
          }, 3000)
        }else {
          wx.showToast({
            title: '预约失败',
            icon: 'success',
            duration: 1000
          });
        }
      });
    }
  }
})